﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace OpenSearchTests
{
    [TestClass]
    public class NasaGhost
    {
        [TestMethod]
        [DeploymentItem("nasa_ghost_osdd.xml")]
        public void TestParseNasaGhostOsdd()
        {
            System.IO.FileStream stream = new System.IO.FileStream("nasa_ghost_osdd.xml", System.IO.FileMode.Open);
            OpenSearch.Osdd.OpenSearchDescriptionDocument osdd = new OpenSearch.Osdd.OpenSearchDescriptionDocument(stream);

            Assert.AreEqual("HyDRO Search", osdd.ShortName);
            Assert.AreEqual("Hydrologic Data Retrieval and Ordering", osdd.LongName);
            Assert.AreEqual("The Hydrologic Data Retrieval and Ordering (HyDRO) system\nis capable of searching the Global Hydrology Resource Center (GHRC) holdings\nfor datasets that meet your criteria of date/time range, lat/lon bounds,\nparameters, instruments, platforms, or projects, resulting in a list of\ngranules (files).  This interface to HyDRO allows you to search for datasets \nbased on date/time range, lat/lon bounds, and free-form search terms.  It \nreturns a list of our online holdings that meet your criteria with links to \ndocumentation and data download.", osdd.Description);

            List<string> expectedTags = new List<string> { "GHRC", "HyDRO", "search" };
            Assert.IsTrue(expectedTags.SequenceEqual(osdd.Tags));

            Assert.AreEqual("ghrcdaac@itsc.uah.edu", osdd.Contact);

            List<OpenSearch.Osdd.Url> urls = osdd.Urls.ToList();
            Assert.AreEqual(1, urls.Count);
            OpenSearch.Osdd.Url url0 = urls[0];
            Assert.AreEqual(1, url0.IndexOffset);
            Assert.AreEqual("application/atom+xml", url0.MimeType);
            Assert.AreEqual("results", url0.Relation);
            Assert.AreEqual("http://ghrcdev.nsstc.nasa.gov/hydro/ghost.pl?q=ds&terms={os:searchTerms?}&aoi={geo:box?}&from={time:start?}&thru={time:end?}&format=atom", url0.Template.ToString());
            OpenSearch.Template template = url0.Template;
            List<OpenSearch.TemplateParameter> parameters = template.TemplateParameters.ToList<OpenSearch.TemplateParameter>();
            Assert.AreEqual(6, parameters.Count);

            OpenSearch.TemplateParameter param0 = parameters[0];
            Assert.AreEqual(OpenSearch.TemplateParameterKind.Literal, param0.Kind);
            Assert.AreEqual("q", param0.Name);
            Assert.AreEqual("ds", param0.ParameterValue);

            OpenSearch.TemplateParameter param1 = parameters[1];
            Assert.AreEqual(OpenSearch.TemplateParameterKind.OptionalSubstitution, param1.Kind);
            Assert.AreEqual("terms", param1.Name);
            Assert.AreEqual(OpenSearch.Constants.NSOpenSearch, param1.SubstitutionParameterValue.Namespace);
            Assert.AreEqual("searchTerms", param1.SubstitutionParameterValue.Parameter);

            OpenSearch.TemplateParameter param2 = parameters[2];
            Assert.AreEqual(OpenSearch.TemplateParameterKind.OptionalSubstitution, param2.Kind);
            Assert.AreEqual("aoi", param2.Name);
            Assert.AreEqual(OpenSearch.Constants.NSOpenSearchGeo, param2.SubstitutionParameterValue.Namespace);
            Assert.AreEqual("box", param2.SubstitutionParameterValue.Parameter);

            OpenSearch.TemplateParameter param3 = parameters[3];
            Assert.AreEqual(OpenSearch.TemplateParameterKind.OptionalSubstitution, param3.Kind);
            Assert.AreEqual("from", param3.Name);
            Assert.AreEqual(OpenSearch.Constants.NSOpenSearchTime, param3.SubstitutionParameterValue.Namespace);
            Assert.AreEqual("start", param3.SubstitutionParameterValue.Parameter);

            OpenSearch.TemplateParameter param4 = parameters[4];
            Assert.AreEqual(OpenSearch.TemplateParameterKind.OptionalSubstitution, param4.Kind);
            Assert.AreEqual("thru", param4.Name);
            Assert.AreEqual(OpenSearch.Constants.NSOpenSearchTime, param4.SubstitutionParameterValue.Namespace);
            Assert.AreEqual("end", param4.SubstitutionParameterValue.Parameter);

            OpenSearch.TemplateParameter param5 = parameters[5];
            Assert.AreEqual(OpenSearch.TemplateParameterKind.Literal, param5.Kind);
            Assert.AreEqual("format", param5.Name);
            Assert.AreEqual("atom", param5.ParameterValue);

            List<OpenSearch.Osdd.Image> images = osdd.Images.ToList();
            Assert.AreEqual(1, images.Count);
            OpenSearch.Osdd.Image image0 = images[0];
            Assert.AreEqual("image/gif", image0.MimeType);
            Assert.AreEqual(48, image0.Height);
            Assert.AreEqual(48, image0.Width);
            Assert.AreEqual("http://ghrcdev.nsstc.nasa.gov/hydro/graphics/square-hydro.gif", image0.Uri.ToString());

            List<OpenSearch.Query> queries = osdd.Queries.ToList();
            Assert.AreEqual(1, queries.Count);
            var q = queries[0];

            OpenSearch.Query query = queries[0];
            Assert.AreEqual("example", query.Role);
            Assert.AreEqual("SSM/I", query.SearchTerms);
            List<OpenSearch.NameSpacedParameter> queryParameters = query.ExtendedAttributes.ToList<OpenSearch.NameSpacedParameter>();
            Assert.AreEqual(2, queryParameters.Count);
            OpenSearch.NameSpacedParameter queryparam0 = queryParameters[0];
            Assert.IsNotNull(queryparam0);
            Assert.AreEqual(OpenSearch.Constants.NSOpenSearch, queryparam0.Namespace);
            Assert.AreEqual("role", queryparam0.Parameter);
            OpenSearch.NameSpacedParameter queryparam1 = queryParameters[1];
            Assert.IsNotNull(queryparam1);
            Assert.AreEqual(OpenSearch.Constants.NSOpenSearch, queryparam1.Namespace);
            Assert.AreEqual("searchTerms", queryparam1.Parameter);

            Assert.AreEqual("Information Technology and Systems Center, The University of Alabama in Huntsville", osdd.Developer);

            Assert.AreEqual("open", osdd.SyndicationRight.ToString());

            Assert.IsFalse(osdd.AdultContent);

            List<string> languages = osdd.Languages.ToList();
            Assert.AreEqual(1, languages.Count);
            Assert.AreEqual("en-us", languages[0]);

            List<string> outputEncodings = osdd.OutputEncodings.ToList();
            Assert.AreEqual(1, outputEncodings.Count);
            Assert.AreEqual("UTF-8", outputEncodings[0]);

            List<string> inputEncodings = osdd.InputEncodings.ToList();
            Assert.AreEqual(1, inputEncodings.Count);
            Assert.AreEqual("UTF-8", inputEncodings[0]);
        }
    }
}